<template>
  <div class="block">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/management/bookmark' }">
        书签列表
      </el-breadcrumb-item>
      <el-breadcrumb-item> 书签详情 </el-breadcrumb-item>
    </el-breadcrumb>

    <el-form
      ref="form"
      :model="bookmark"
      status-icon
      :rules="rules"
      label-width="5rem"
      class="bookmark-detail"
      label-position="left"
    >
      <el-form-item label="图标">
        <el-upload
          class="avatar-uploader"
          list-type="picture-card"
          action="http://localhost:8083/api/Upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="名称">
        <el-input class="bookmark-input" v-model="bookmark.name"></el-input>
      </el-form-item>

      <el-form-item label="类型">
        <el-select
          class="bookmark-input"
          v-model="bookmark.type"
          placeholder="请选择活动区域"
        >
          <el-option
            v-for="item in types"
            v-bind:lable="item.name"
            v-bind:key="item.id"
            v-bind:value="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="链接" prop="url">
        <el-input class="bookmark-input" v-model="bookmark.url">
          <!-- <template slot="prepend">Http://</template> -->
        </el-input>
      </el-form-item>
    </el-form>

    <el-button v-if="saveButton" @click="addBookmark" type="primary"
      >保存</el-button
    >
    <el-button @click="toBookmark" type="primary" plain>返回</el-button>
  </div>
</template>

<script>
import { saveBookMark, getBookMarkItem, getBookMarkType } from '@/api/bookmark';

export default {
  name: 'bookmarkdetail',
  data () {

    // <!--检验网址地址是否合法->
    var checkUrl = (rule, value, callback) => {
      var reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/;
      debugger;
      if (!reg.test(value)) {
        return callback(new Error('请输入正确的网址'))
      } else {
        return callback()
      }

    }
    return {
      bookmark: {
        name: "",
        icon: "",
        url: "",
      },
      types: [],
      imageUrl: '',
      saveButton: true,
      rules: {
        url: [
          { type: 'string', required: true, validator: checkUrl, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    addBookmark () {
      var _this = this;
      debugger;
      saveBookMark(_this.bookmark).then(function (value) {
        console.log(value);
        _this.$router.push("/management/bookmark")
      }, function (error) {
        console.log(error);
      });
    },
    toBookmark () {
      this.$router.push("/management/bookmark")
    },
    getBookmark (id) {
      debugger;
      var _this = this;
      getBookMarkItem(id).then(function (value) {
        console.log(value);
        debugger;
        _this.imageUrl = value.data[0].icon;
        _this.bookmark = value.data[0];
      }, function (error) {
        console.log(error);
      });
    },
    getBookMarkType () {
      var _this = this;
      getBookMarkType().then(function (value) {
        console.log(value);
        _this.types = value.data

      }, function (error) {
        console.log(error);
      });
    },
    handleAvatarSuccess (res, file) {
      const baseImgUrl = config.baseImgUrl;
      //this.imageUrl = URL.createObjectURL(file.raw);
      this.bookmark.icon = baseImgUrl + "/" + res.newFileName;
      this.imageUrl = baseImgUrl + "/" + res.newFileName;
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
  mounted () {
    var params = this.$route.params;
    console.log(params);
    debugger;
    if (params.type == 'show') {
      this.getBookmark(params.id);
      this.saveButton = false;

    }
    else if (params.type == 'edit') {
      this.getBookmark(params.id);

    }
    this.getBookMarkType();
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.block {
  border: 1px solid #ebebeb;
  border-radius: 3px;
  transition: 0.2s;
}
.bookmark-detail {
  margin-top: 50px;
  width: 500px;
  margin-left: auto;
  margin-right: auto;
}
.bookmark-input {
  width: 500px;
}
.avatar {
  width: 148px;
  height: 148px;
}
</style>
